<?php

namespace App\Http\Controllers;
use Mail;
use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class RegController extends Controller
{
    //前台注册验证用户名
    public function getReg()
    {
        return view('login.reg');
    }
    //前台注册验证用户名
    public function getYhm(Request $request)
    {
        $user = DB::table('users')->where('username',$request->input('username'))
        ->where('state',0)
        ->first();
        if($user){
            return 1; //用户已存在
        }else{
            return 0; //用户不存在
        }
    }

    public function getLogin()
    {
    	return view('login.weblogin');
    }
    //用户登录
    public function postDenglu(Request $request)
    {
        // IndexController::getIndex();
        $user = DB::table('users')->where('username',$request->input('username'))
        ->where('state','0')
        ->first();
        if(!$user){
            return back()->with('error','用户名或密码错误');
        }else{
            if(Hash::check($request->input('password'),$user->password)){
                //登录成功 将用户id放入session中
            session(['id'=>$user->id,'username'=>$user->username]);
            return redirect('/');
            }else{
                return back()->with('error','用户名或密码错误');
            }
        }
    }
    //前台用户重置密码
    public function getFind()
    {
        return view('login.find');
    }
    //检查输入的用户名是否有效
    public function getCheckemail(Request $request)
    {
        $email = $request->input('email');
        $res = DB::table('users')->where('email',$email)->first();
        if($res){
            echo 1;  //用户名存在 可以找回
        }else{
            echo 0;  //用户名不存在
        }
    }

    //发送邮箱验证码
    public function getEmail(Request $request)
    {
        $email = $request->input('email');

        $str = rand(10000,99999);
        echo $str;
        // dd($email);
        Mail::raw('尊敬的用户你的验证码是'.$str, function ($message)use($email) {
             //设置邮件标题
            $message->subject('哈哈马密码重置邮件'); 
             //发送给谁 
            $message->to($email); 
            });
    }
    //密码重置邮箱验证页面
    public function postMima(Request $request)
    {
        $email = $request->input('email');
        // echo $email;
        $res = DB::table('users')->where('email',$email)->first();
        if(!$res){
            return back()->with('error','邮箱不正确');
        }else{
            // session(['usernameid'=>$res->id]);
            $id = $res->id;
            return view('login.change',['res'=>$id]);
        }
    }
    //密码重置页面
    public function postChange(Request $request)
    {
        // dd($request->only('password','id'));
        $pass = $request->input('password');
        $id = $request->input('id');
        $data = [];
        $data['password'] = Hash::make($pass);
        $res = DB::table('users')->where('id',$id)->update($data);
        if($res){
            return view('login.mima');
        }else{
            return back()->with('error','密码重置失败');
        }

    }

    //退出登录
    public function getClear(Request $request)
    {
        $request->session()->flush();
        return redirect('/');
    }

}
